<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/11/19
 * Time: 16:08
 */

namespace app\api\controller;


use app\api\BaseController;
use app\common\model\Article;
use app\common\model\Pbundling;

class Building extends BaseController
{

    public function select(Pbundling $pbundling_model){
        $where = array();
//        $where['store_id'] = session('store_id');
        if (input('param.bundling_name') != '') {
            $where['bl_name'] = array('like', '%' . trim(input('param.bundling_name')) . '%');
        }
        if (is_numeric(input('param.state'))) {
            $where['bl_state'] = input('param.state');
        }
        $bundling_list = $pbundling_model->getBundlingList($where, '*', 'bl_id desc', 10, 0, '');
        $bundling_list = array_under_reset($bundling_list, 'bl_id');
        if (!empty($bundling_list)) {
            $blid_array = array_keys($bundling_list);

            $bgoods_array = $pbundling_model->getBundlingGoodsList(array('bl_id' => array('in', $blid_array), 'blgoods_appoint' => 1), 'bl_id,goods_id,count(*) as count', 'blgoods_appoint desc', 'bl_id');
            $bgoods_array1 = array_under_reset($bgoods_array, 'goods_id');

            if (!empty($bgoods_array1)) {
                $goodsid_array = array_keys($bgoods_array1);
                $goods_array = model('goods')->getGoodsList(array(
                    'goods_id' => array('in', $goodsid_array)), 'goods_id,goods_image');
                $goods_array = array_under_reset($goods_array, 'goods_id');
            }
            $bgoods_array = array_under_reset($bgoods_array, 'bl_id');

            foreach ($bundling_list as $key => $val) {
                $bundling_list[$key]['goods_id'] = $bgoods_array[$val['bl_id']]['goods_id'];
                $bundling_list[$key]['count'] = $bgoods_array[$val['bl_id']]['count'];
                $bundling_list[$key]['img'] = goods_thumb($goods_array[$bgoods_array[$val['bl_id']]['goods_id']], 60);
            }
        }

        json_success_return($bundling_list);

    }

    public function detail(Pbundling $pbundling,Article $article,$id){
        $bundling_info = $pbundling->getBundlingInfo(array('bl_id' => $id));
        $b_goods_list = $pbundling->getBundlingGoodsList(array('bl_id' => $id));
        $goodsid_array = array();
        $goods_list = array();
        if (!empty($b_goods_list)) {
            foreach ($b_goods_list as $val) {
                $goodsid_array[] = $val['goods_id'];
            }
            $goods_list = model('goods')->getGoodsList(array('goods_id' => array('in', $goodsid_array)), 'goods_id,goods_price,goods_image,goods_name');
        }
        $articleInfo = $article->getOneArticle(['article_id'=>$bundling_info['article_id']]);

        $data['bundling_info'] = $bundling_info;
        $data['goods'] = $goods_list;
        $data['article'] = $articleInfo;

        json_success_return($data);
    }

}